package com.example.module_filemanage.Adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.module_filemanage.Interface.IFileInfoOnclickListener;
import com.example.module_filemanage.MVVM.Module.Entil.Item_FileInfo_module;
import com.example.module_filemanage.Public.BodyColor;
import com.example.module_filemanage.R;
import com.example.module_filemanage.databinding.FmItemFileinfo2Binding;
import com.example.module_filemanage.databinding.FmItemFileinfoBinding;

import java.util.ArrayList;

/**
 * Created by SJ on 2018/4/20.
 */

public class FileInfo_Adapter extends RecyclerView.Adapter<ViewHolder> {

    ArrayList<Item_FileInfo_module> _items;
    private IFileInfoOnclickListener mClickListener;
    ArrayList<Item_FileInfo_module> items;
    public FileInfo_Adapter(ArrayList<Item_FileInfo_module> item){
        super();
        this._items=item;
        this.items=item;
    }

    public void setItems(ArrayList<Item_FileInfo_module> IItems) {
        _items=IItems;
        this.items=IItems;
    }

    @Override
    public int getItemViewType(int position) {
        return _items.get(position).isIspic()?0:1;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        FileInfo_ViewHolder holder=null;
        switch (viewType){
            case 0:
                FmItemFileinfo2Binding binding2 = DataBindingUtil.inflate(LayoutInflater
                        .from(parent.getContext()), R.layout.fm_item_fileinfo2, parent, false);
                binding2.frameItem.setBackgroundResource(BodyColor.left_menu_body);
                holder = new FileInfo_ViewHolder(binding2);
                holder.setDataBinding(binding2);
                break;
            case 1:
                FmItemFileinfoBinding binding = DataBindingUtil.inflate(LayoutInflater
                        .from(parent.getContext()), R.layout.fm_item_fileinfo, parent, false);
                binding.frameItem2.setBackgroundResource(BodyColor.left_menu_body);
                holder = new FileInfo_ViewHolder(binding);
                holder.setDataBinding(binding);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        FileInfo_ViewHolder _fileInfo_viewHolder=(FileInfo_ViewHolder)holder;
      if (_fileInfo_viewHolder.getDataBinding() instanceof FmItemFileinfo2Binding){
          FmItemFileinfo2Binding _binding=((FmItemFileinfo2Binding) _fileInfo_viewHolder.getDataBinding());
          _binding.setItem(_items.get(position));
          _binding.imageView5.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View IView) {
                  if (mClickListener!=null){
                      mClickListener.deletListener(IView,position,_items.get(position));
                  }
              }
          });
          _binding.imageView6.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View IView) {
                  if (mClickListener!=null){
                      mClickListener.downloadListener(IView,position);
                  }
              }
          });
          _binding.imageView4.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View IView) {
                  if ( mClickListener!=null){
                      mClickListener.showimgListener(IView,position);
                  }

              }
          });
          _binding.frameItem.setOnLongClickListener(new View.OnLongClickListener() {
              @Override
              public boolean onLongClick(View IView) {
                  if ( mClickListener!=null){
                      mClickListener.onclickListener(IView,position);
                  }
                  return false;
              }
          });
      }else if (_fileInfo_viewHolder.getDataBinding() instanceof FmItemFileinfoBinding){
          FmItemFileinfoBinding _binding= ((FmItemFileinfoBinding) _fileInfo_viewHolder.getDataBinding());
          _binding.setItem(_items.get(position));
          _binding.imageView3.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View IView) {
                  if (mClickListener!=null){
                      mClickListener.deletListener(IView,position,_items.get(position));
                  }
              }
          });
          _binding.imageView2.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View IView) {
                  if (mClickListener!=null){
                      mClickListener.downloadListener(IView,position);
                  }
              }
          });
          _binding.frameItem2.setOnLongClickListener(new View.OnLongClickListener() {
              @Override
              public boolean onLongClick(View IView) {
                  if ( mClickListener!=null){
                      mClickListener.onclickListener(IView,position);
                  }
                  return false;
              }
          });

      }
        _fileInfo_viewHolder.getDataBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return _items.size();
    }

   static class FileInfo_ViewHolder extends ViewHolder{
       ViewDataBinding mDataBinding;
       public FileInfo_ViewHolder(ViewDataBinding mDataBinding) {
           super(mDataBinding.getRoot());
       }
       public ViewDataBinding getDataBinding() {
           return mDataBinding;
       }

       public void setDataBinding(ViewDataBinding IDataBinding) {
           mDataBinding=IDataBinding;
       }

   }

   //设置item子项监听事件
   public void setFileOnclickListener(IFileInfoOnclickListener Iclicklistener){
       this.mClickListener=Iclicklistener;
   }
   //删除子项item
    public void delFileItem(int position){
       _items.remove(position);
       notifyDataSetChanged();
    }
    public void selectName(String name){
        _items=new ArrayList<>();
        for (Item_FileInfo_module item:
             items) {
            if (item.account.equals(name)){
                _items.add(item);
            }
        }
        notifyDataSetChanged();
    }
    public void selectAllname(){
        _items=new ArrayList<>(items);
        notifyDataSetChanged();
    }
    public Item_FileInfo_module getitem(int position){
        return _items.get(position);
    }
    public void changename(int position,String key){
        Item_FileInfo_module _item_fileInfo_module=_items.get(position);
        _item_fileInfo_module.setFilename(key);
        _items.set( position,_item_fileInfo_module);
        notifyDataSetChanged();
    }

}
